import xml.dom.minidom


class Noname:
    def __init__(self):
        pass


class Result:
    def __init__(self):
        self.loads = list()


class Parser:
    def __init__(self, env):
        self.env = env

    def _name(self, a):
        name = a.getAttribute('name')
        if name:
            return self.env.escape(name)
        else:
            return None

    def _category(self, a):
        category = a.getAttribute('category')
        if category:
            return self.env.escape(category)
        else:
            return None

    def parse(self, xmlfile):
        results = list()

        root = xml.dom.minidom.parse(xmlfile)
        if root.nodeName == 'atest':
            return results

        for c in root.childNodes[0].childNodes:
            if c.nodeName != 'case':
                continue

            result = Result()
            result.name = c.getAttribute('name')
            result.category = self._category(c)
            for a in c.childNodes:
                if a.nodeName == 'load':
                    r = Noname()
                    r.name = self._name(a)
                    r.category = self._category(a) or result.category
                    result.loads.append(r)
                elif a.nodeName == 'source':
                    result.source = self._name(a)
                elif a.nodeName == 'target':
                    result.target = self._name(a)

            results.append(result)

        return results
